package edu.must.uh.Filter;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSON;
import edu.must.uh.util.CookieUtil;
import edu.must.uh.util.JedisUtil;
import edu.must.uh.util.R;
import edu.must.uh.util.Tools;



public class FilterLogin implements Filter {

    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    public void doFilter(ServletRequest request, ServletResponse resp,
                         FilterChain filterChain) throws ServletException,
            IOException {
        
       HttpServletResponse response = (HttpServletResponse) resp;  
       request.setCharacterEncoding("UTF-8");       
       response.setCharacterEncoding("UTF-8"); 
       
       response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域访问报错 
       response.setHeader("Access-Control-Allow-Methods", "POST, GET"); 
       response.setHeader("Access-Control-Max-Age", "3600"); //设置过期时间 
       response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,cookieskey,LoginUser"); 
       response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1. 
       response.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
       
       HttpServletRequest httpServletRequest = (HttpServletRequest) request;
       
       
       String servletPath = httpServletRequest.getServletPath(); 
       filterChain.doFilter(request, response);
       /*  if (     servletPath.endsWith("/login.jsp")
               || servletPath.endsWith("/LoginServlet")  
               || servletPath.endsWith(".js") 
               || servletPath.endsWith(".css")
               || servletPath.contains("images")
               || servletPath.contains(".json")
               || servletPath.contains("index.html")
               || servletPath.contains("console.html")
             
               
           )  
       {     
          //否则继续执行     
                    
       }else {
           String cookiesUser = CookieUtil.getUid((HttpServletRequest)request, "LoginUser");
           String cookieskey = CookieUtil.getUid((HttpServletRequest)request, "cookieskey");
           
           String  LoginUser = JedisUtil.get("LoginUser",cookiesUser);  
           User user = JSON.parseObject(LoginUser, User.class); 
           if(user==null ||cookieskey == null || !user.getEncryptkey().equals(cookieskey)){
               PrintWriter out=response.getWriter();      
               out.print(JSON.toJSONString(R.fail("unLogin")));
           }else{                
               System.out.println(JSON.toJSONString(user));   
               filterChain.doFilter(request, response);//否则继续执行
           }           
       }*/
    }
    public void destroy() {
    }

}
